import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import common._
import helpers._
import models._
import mongodb.MongodbContext
import mqtt.MQTTContext
import routes._

import scala.concurrent.ExecutionContext
import scala.util.Random

object AppLoader extends App with Configurable {

  override val configuration: Configuration = Configuration.load()

  implicit lazy val _Mongodb: MongodbContext = new MongodbContext(configuration)
  implicit val ec: ExecutionContext          = scala.concurrent.ExecutionContext.Implicits.global

  val boot = new Bootstrap {
    override def bootup(
        implicit
        system: ActorSystem,
        materializer: ActorMaterializer
    ): List[RoutesDefinition] = {

//      implicit val _GzhInfos: GzhInfos = new GzhInfos()
//
//      val internalHealth = new InternalHealthRoutes()
//      val gzhInfoRoutes  = new GzhInfoRoutes()
//
//      List(internalHealth, gzhInfoRoutes)

      List(new InternalHealthRoutes())
    }
  }
  new HttpServer(boot, configuration, "admin-management")

//  import org.fusesource.mqtt.client._
//
//  val _MQTTContext = new MQTTContext(configuration)
//
//  val _BlockingConnection = _MQTTContext.blockingConnection
//
//  _BlockingConnection.connect()
//  _BlockingConnection.publish("MAC -02", "Hello".getBytes, QoS.AT_LEAST_ONCE, false)
//
//  _BlockingConnection.subscribe(
//    Array(new Topic("apple", QoS.valueOf("AT_LEAST_ONCE")), new Topic("orange", QoS.valueOf("AT_MOST_ONCE")), new Topic("orange", QoS.valueOf("AT_MOST_ONCE")))
//  )

//  val message = _BlockingConnection.receive()
//  println(new String(message.getPayload))
//  message.ack()

//  _BlockingConnection.publish("Apple", "Hello World".getBytes, QoS.AT_MOST_ONCE, false)
//  _BlockingConnection.publish("apple", "Fruit Name".getBytes, QoS.AT_LEAST_ONCE, false)
//  _BlockingConnection.publish(
//    "apple/iphone",
//    """{"price": "$ 599", "inventory": 99}""".getBytes,
//    QoS.EXACTLY_ONCE,
//    false
//  )

//  _BlockingConnection.disconnect()

//  try {
//    println(connection.receive())
//  } catch {
//    case e: Throwable ⇒ println(e.getMessage)
//  }

//  val _CallbackConnection = _MQTTContext.callbackConnection
//
//  _CallbackConnection.listener(new Listener() {
//    override def onConnected(): Unit = println("connected")
//
//    override def onDisconnected(): Unit = println("disconnected")
//
//    override def onPublish(topic: UTF8Buffer, body: Buffer, ack: Runnable): Unit = ack.run()
//
//    override def onFailure(value: Throwable): Unit = _CallbackConnection.disconnect(null)
//  })
//

//  _CallbackConnection.connect(new Callback[Void] {
//
//    def onSuccess(value: Void): Unit = {
//      println("链接成功")
//      _CallbackConnection.subscribe(
//        Array(
//          new Topic("apple", QoS.valueOf("AT_LEAST_ONCE")),
//          new Topic("orange", QoS.valueOf("AT_MOST_ONCE")),
//          new Topic("banana", QoS.valueOf("EXACTLY_ONCE"))
//        ),
//        new Callback[Array[Byte]] {
//
//          def onSuccess(value: Array[Byte]): Unit =
//            println(new String(value))
//
//          def onFailure(value: Throwable): Unit =
//            println(s"""Failure on subscribes Topics["apple", "orange", "banana"]""", value.getMessage)
//          _CallbackConnection.disconnect(null)
//        }
//      )
//    }
//
//
//    def onFailure(value: Throwable): Unit = println(s"""Failure on connect""", value.getMessage)
//  })
}
